package com.mingshine.cloud.sender;


import com.mingshine.cloud.config.ExchangeConfig;
import com.mingshine.cloud.config.RabbitMqConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;


@Component
public class RabbitMqSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 发送消息
     *
     * @param uuid
     * @param message 消息
     */
    public void send(String uuid, String message) {
        CorrelationData correlationId = new CorrelationData(uuid);
        /**
         * ExchangeConfig.FANOUT_EXCHANGE  指定消息交换机
         * RabbitMqConfig.ROUTIN_FANOUT_KEY  指定一个路由键，不过其实fanout不需要路由键
         */
        //发送消息至：direct类型的交互机【ExchangeConfig.FANOUT_EXCHANGE】上，路由键是【RabbitMqConfig.ROUTIN_FANOUT_KEY】
        logger.error("=======DeviceMinaMqSender=======" + message);
        rabbitTemplate.convertAndSend(ExchangeConfig.TEST_EXCHANGE, RabbitMqConfig.ROUTE_TEST_KEY,
                message, correlationId);
        //TODO 绑定在此交换机上的队列都会收到数据，监听此队列的消费者都会收到信息
    }

    /**
     * 发送消息
     *
     * @param message 消息
     */
    public void send(String message) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString().replace("-", ""));
        /**
         * ExchangeConfig.FANOUT_EXCHANGE  指定消息交换机
         * RabbitMqConfig.ROUTIN_FANOUT_KEY  指定一个路由键，不过其实fanout不需要路由键
         */
        //发送消息至：direct类型的交互机【ExchangeConfig.FANOUT_EXCHANGE】上，路由键是【RabbitMqConfig.ROUTIN_FANOUT_KEY】
        logger.error("=======DeviceMinaMqSender=======" + message);
        rabbitTemplate.convertAndSend(ExchangeConfig.TEST_EXCHANGE, RabbitMqConfig.ROUTE_TEST_KEY,
                message, correlationId);
        //TODO 绑定在此交换机上的队列都会收到数据，监听此队列的消费者都会收到信息
    }


}